package com.shqcjd.preinstallglass.ui.activity.remote;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;

import com.open.androidtvwidget.bridge.EffectNoDrawBridge;
import com.open.androidtvwidget.bridge.OpenEffectBridge;
import com.open.androidtvwidget.view.FrameMainLayout;
import com.open.androidtvwidget.view.MainUpView;
import com.orhanobut.logger.Logger;
import com.shqcjd.preinstallglass.R;
import com.shqcjd.preinstallglass.ui.base.BaseActivity;
import com.shqcjd.preinstallglass.ui.base.BasePresenter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.kareluo.intensify.image.IntensifyImageView;

public class BigImageLoaderActivity extends BaseActivity {
    @BindView(R.id.intensify_image)
    IntensifyImageView intensifyImage;
    @BindView(R.id.btn_move_l)
    Button btn_move_l;
    @BindView(R.id.btn_zoom)
    Button btnZoom;
    @BindView(R.id.btn_move_r)
    Button btnMoveR;
    @BindView(R.id.btn_move_up)
    Button btnMoveUp;
    @BindView(R.id.btn_move_d)
    Button btnMoveD;
    @BindView(R.id.btn_init)
    Button btn_init;
    private Context mContext;
    private String imgurl;

    @BindView(R.id.mainUpView1)
    MainUpView mMainUpView;
    @BindView(R.id.main_lay)
    FrameMainLayout main_lay11;
    OpenEffectBridge mOpenEffectBridge;
    View mOldFocus;
    private float initScale;
    private boolean isinit = true;

    @Override
    protected boolean isFullScreen() {
        return true;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_big_image_loader;
    }

    @Override
    protected String getVoiceContent() {
        return "|返回|放大|向上|向下|向左|向右;";
    }

    @Override
    protected void onVoiceResult(String result) {
        Logger.i("识别结果:"+result);
        switch (result) {
            case "返回":
                finish();
                break;
            case "放大":
                btnZoom.performClick();
                break;
            case "向上":
                btnMoveUp.performClick();
                break;
            case "向下":
                btnMoveD.performClick();
                break;
            case "向左":
                btn_move_l.performClick();
                break;
            case "向右":
                btnMoveR.performClick();
                break;
        }
    }

    @Override
    public void initView() {
        mContext = this;
        imgurl = getIntent().getStringExtra("imgurl");
        Uri fileuri = Uri.parse(imgurl);

        intensifyImage.setImage(new File(fileuri.getPath()));

        mMainUpView.setEffectBridge(new EffectNoDrawBridge());
        mOpenEffectBridge = (OpenEffectBridge) mMainUpView.getEffectBridge();
        Drawable testDrawable = null;
        mMainUpView.setUpRectDrawable(testDrawable); // 设置边框图片
        mMainUpView.setUpRectResource(R.drawable.test_rectangle); // 设置边框图片.


        main_lay11.getViewTreeObserver().addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(final View oldFocus, final View newFocus) {

                if (newFocus != null)
//                    newFocus.bringToFront(); // 防止放大的view被压在下面. (建议使用MainLayout)
                    mMainUpView.setFocusView(newFocus, mOldFocus, 1.0f);
                mOldFocus = newFocus; // 4.3以下需要自己保存.
                // 测试是否让边框绘制在下面，还是上面. (建议不要使用此函数)
//                if (newFocus != null) {
//                    testTopDemo(newFocus, scale);
//                }
            }
        });


    }


    @OnClick({R.id.btn_move_l, R.id.btn_zoom,R.id.btn_move_r, R.id.btn_move_up, R.id.btn_move_d,R.id.btn_init})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_zoom:
                if (isinit) {
                    initScale = intensifyImage.getScale();
                    isinit =false;
                }
                float scale = intensifyImage.getScale();
                if (scale < 1.5f) {
                    intensifyImage.setScale(scale + 0.3f);
                }
                break;
            case R.id.btn_move_l:
                intensifyImage.setScroll(-250, 0);
                break;
            case R.id.btn_move_r:
                intensifyImage.setScroll(250, 0);
                break;
            case R.id.btn_move_up:
                intensifyImage.setScroll(0, -250);
                break;
            case R.id.btn_move_d:
                intensifyImage.setScroll(0, 250);
                break;
            case R.id.btn_init:
                if (initScale == 0) {
                    return;
                }
                intensifyImage.setScale(initScale);
                break;
        }
    }
}
